package com.xy.service.mainData;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xy.api.exception.BizException;
import com.xy.api.model.dto.mainData.ProductInstanceDTO;
import com.xy.api.model.request.mainData.ProductRequest;
import com.xy.api.model.request.mainData.SubmitProductInstanceRequest;
import com.xy.api.model.response.Rows;
import com.xy.api.service.mainData.IProductInstanceService;
import com.xy.mapper.mainData.ProductInstanceMapper;
import com.xy.mapper.mainData.ProductMapper;
import com.xy.model.po.mainData.ProductInstancePO;
import com.xy.model.po.mainData.ProductPO;
import com.xy.utils.CustomBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductInstanceServiceImpl implements IProductInstanceService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductInstanceMapper productInstanceMapper;

    @Override
    public Rows<ProductInstanceDTO> queryProductInstanceList(ProductRequest request) {
        Assert.notNull(request.getId(), "产品ID必填");

        LambdaQueryWrapper<ProductInstancePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(ProductInstancePO::getIsDeleted, false)
                .eq(ProductInstancePO::getProductId, request.getId());
        List<ProductInstancePO> productInstancePOS = productInstanceMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(productInstancePOS)) {
            return new Rows<>(new ArrayList<>(), 0);
        }
        List<ProductInstanceDTO> productInstanceList = CustomBeanUtil.copyListProperties(productInstancePOS, ProductInstanceDTO::new);
        return new Rows<>(productInstanceList, productInstanceList.size());
    }

    @Override
    @Transactional
    public void saveProductInstanceList(SubmitProductInstanceRequest request) {
        Assert.notNull(request.getProductId(), "产品ID必填");
        Assert.notEmpty(request.getInstanceList(), "产品实例不能为空");

        ProductPO productPO = productMapper.selectById(request.getProductId());
        if (null == productPO) {
            throw new BizException("产品不存在");
        }
        List<ProductInstancePO> records = CustomBeanUtil.copyListProperties(request.getInstanceList(), ProductInstancePO::new);
        for (ProductInstancePO productInstance : records) {
            productInstance.setProductId(productPO.getId());
            productInstance.setProductName(productPO.getName());
            if (null == productInstance.getId()) {
                productInstanceMapper.insert(productInstance);
            } else {
                productInstanceMapper.updateById(productInstance);
            }
        }
    }

    @Override
    public void deleteProductInstance(Long id) {
        Assert.notNull(id, "产品实例ID必填");

        ProductInstancePO deleteRecord = new ProductInstancePO();
        deleteRecord.setId(id);
        deleteRecord.setIsDeleted(true);
        productInstanceMapper.updateById(deleteRecord);
    }

    @Override
    public List<ProductInstanceDTO> queryProductInstanceByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<ProductInstancePO> query = new LambdaQueryWrapper<>();
        query.eq(ProductInstancePO::getIsDeleted, 0)
                .in(ProductInstancePO::getId, ids);
        List<ProductInstancePO> productInstancePOS = productInstanceMapper.selectList(query);
        return CustomBeanUtil.copyListProperties(productInstancePOS, ProductInstanceDTO::new);
    }

    @Override
    public List<ProductInstanceDTO> queryProductInstanceByProductIds(List<Long> productIds) {
        if (CollectionUtils.isEmpty(productIds)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<ProductInstancePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(ProductInstancePO::getIsDeleted, false)
                .in(ProductInstancePO::getProductId, productIds);
        List<ProductInstancePO> productInstancePOS = productInstanceMapper.selectList(queryWrapper);
        return CustomBeanUtil.copyListProperties(productInstancePOS, ProductInstanceDTO::new);
    }
}
